package duplicateur;


import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nathalie
 */


public class InterfaceDuplicateur extends javax.swing.JFrame {
    private Duplicateur duplicateur;
    private File[] selectedFile;
    private String nomDossier;
    private File[] supports;
    private ArrayList allSupports;
    private ArrayList allDisquesAmvibles;
    private JCheckBox[] tabCheckBox;
    private JProgressBar[] tabProgressBar;
    private JProgressBar[] selectedProgressBar;
    private File dossier;
    int nbThread;
    int nbThreadEnd;
    /**
     * Creates new form NewJFrame
     */
    public InterfaceDuplicateur() {
        initComponents();
        this.tabCheckBox = new JCheckBox[]{this.jCheckBox1, this.jCheckBox2, this.jCheckBox3, this.jCheckBox4, this.jCheckBox5, this.jCheckBox6, this.jCheckBox7, this.jCheckBox8, this.jCheckBox9, this.jCheckBox10, this.jCheckBox11, this.jCheckBox12};
        this.tabProgressBar = new JProgressBar[]{this.jProgressBar1, this.jProgressBar2, this.jProgressBar3, this.jProgressBar4, this.jProgressBar5, this.jProgressBar6, this.jProgressBar7, this.jProgressBar8, this.jProgressBar9, this.jProgressBar10, this.jProgressBar11, this.jProgressBar12};
        this.duplicateur = new Duplicateur();
        this.selectedFile = null;
        updateSupports();
        this.allDisquesAmvibles = duplicateur.getAllDisquesAmovibles();
        this.jTextAreaEtat.setText("Bienvenue!");
        ThreadUpdateSupport threadUpdate = new ThreadUpdateSupport(this);
        threadUpdate.start();
        nbThread = 0;
        nbThreadEnd = 0;
    }
    
   public void updateSupports() {
       this.allSupports = duplicateur.getAllSupport();
       for(JCheckBox c : tabCheckBox) {
            c.hide();
        }
        
        for(JProgressBar p : tabProgressBar) {
            p.hide();
        }
       
        for (int i = 0; i < allSupports.size(); i++) {
            tabCheckBox[i].show();
            tabProgressBar[i].show();
            tabCheckBox[i].setText((String)allSupports.get(i));
        }
        this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Mise à jour des supports");
        this.getJTextAreaEtat().setCaretPosition(this.getJTextAreaEtat().getDocument().getLength());
   }
   
   public Duplicateur getDuplicateur() {
       return this.duplicateur;
   }
            
class AfficheurFlux implements Runnable {

    private final InputStream inputStream;
    private final JTextArea jTextAreaEtat;
    private final String diskAFormater;

    AfficheurFlux(InputStream inputStream, JTextArea jTextAreaEtat, String diskAFormater) {
        this.inputStream = inputStream;
        this.jTextAreaEtat = jTextAreaEtat;
        this.diskAFormater = diskAFormater;
    }


    private BufferedReader getBufferedReader(InputStream is) {
        return new BufferedReader(new InputStreamReader(is));
    }

    @Override
    public void run() {
        BufferedReader br = getBufferedReader(inputStream);
        String ligne;
        try {
            while ((ligne = br.readLine()) != null) {
                System.out.println(ligne); 
                int finl = ligne.indexOf(" ");
                int testfin = ligne.indexOf("rie du volume est");
                int testfin2 = ligne.indexOf("Formatage termin");
                int testfin3 = ligne.indexOf("Le volume est trop grand pour");
                String textarea = jTextAreaEtat.getText() + "\n";
                if(finl != -1){
                    String percent = ligne.substring(0, finl);
                    try {
                        int pourcent = Integer.parseInt(percent);
                        jTextAreaEtat.setText(textarea + "Avancement : " + percent.toString());
                    } catch (NumberFormatException nfe) {
                        // traitement à faire dans ce cas
                        if(testfin != -1 || testfin2 != -1){
                            jTextAreaEtat.setText(textarea + "Formatage de " + diskAFormater + "  Terminé"); 
                        }
                        else if(testfin3 != -1){
                            jTextAreaEtat.setText(textarea + diskAFormater + " Echec, volume trop grand!"); 
                        }
                    } 
                }
            }
        } catch (IOException e) {
        }
    }
    }
    
    private void getSelectedSupports() {
        int j = 0;
        String[] supports = new String[11];
        this.selectedProgressBar = new JProgressBar[11];
        for(int i = 0; i < 12; i++) {
            if(this.tabCheckBox[i].isSelected()) {
                supports[j] = (String)allSupports.get(i);
                this.selectedProgressBar[j] = tabProgressBar[i];
                j++;
                
            }
        }
        
        this.supports = this.duplicateur.supportsStringToFile(supports);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        scrollEtat = new javax.swing.JScrollPane();
        jTextAreaEtat = new javax.swing.JTextArea();
        labelDestination = new javax.swing.JLabel();
        labelSource = new javax.swing.JLabel();
        boutonCopier = new javax.swing.JButton();
        boutonParcourir = new javax.swing.JButton();
        boutonNouveauDossier = new javax.swing.JButton();
        boutonQuitter = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jProgressBar2 = new javax.swing.JProgressBar();
        jCheckBox3 = new javax.swing.JCheckBox();
        jProgressBar4 = new javax.swing.JProgressBar();
        jProgressBar3 = new javax.swing.JProgressBar();
        jProgressBar5 = new javax.swing.JProgressBar();
        jProgressBar6 = new javax.swing.JProgressBar();
        jCheckBox10 = new javax.swing.JCheckBox();
        jCheckBox11 = new javax.swing.JCheckBox();
        jCheckBox12 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jPanel3 = new javax.swing.JPanel();
        jProgressBar7 = new javax.swing.JProgressBar();
        jProgressBar8 = new javax.swing.JProgressBar();
        jProgressBar9 = new javax.swing.JProgressBar();
        jProgressBar10 = new javax.swing.JProgressBar();
        jProgressBar11 = new javax.swing.JProgressBar();
        jProgressBar12 = new javax.swing.JProgressBar();
        jCheckBox8 = new javax.swing.JCheckBox();
        jCheckBox7 = new javax.swing.JCheckBox();
        jCheckBox9 = new javax.swing.JCheckBox();
        barreMenu = new javax.swing.JMenuBar();
        actions = new javax.swing.JMenu();
        Copier = new javax.swing.JMenuItem();
        EjecterLesClesUSB = new javax.swing.JMenuItem();
        Formater = new javax.swing.JMenuItem();
        FormaterNTFS = new javax.swing.JMenuItem();
        FormaterFAT32 = new javax.swing.JMenuItem();
        FormaterFAT = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextAreaEtat.setEditable(false);
        jTextAreaEtat.setColumns(20);
        jTextAreaEtat.setRows(5);
        scrollEtat.setViewportView(jTextAreaEtat);

        labelDestination.setText("Destination (par défaut à la racine) :");

        labelSource.setText("Source :");

        boutonCopier.setText("Copier");
        boutonCopier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonCopierActionPerformed(evt);
            }
        });

        boutonParcourir.setText("Parcourir");
        boutonParcourir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonParcourirActionPerformed(evt);
            }
        });

        boutonNouveauDossier.setText("Parcourir");
        boutonNouveauDossier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonNouveauDossierActionPerformed(evt);
            }
        });

        boutonQuitter.setText("Quitter");
        boutonQuitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonQuitterActionPerformed(evt);
            }
        });

        jCheckBox1.setText("Disque Amovible 1");

        jCheckBox2.setText("Disque Amovible 2");

        jCheckBox3.setText("jCheckBox1");

        jCheckBox10.setText("jCheckBox5");

        jCheckBox11.setText("jCheckBox6");

        jCheckBox12.setText("jCheckBox7");

        jCheckBox4.setText("jCheckBox8");

        jCheckBox5.setText("jCheckBox9");

        jCheckBox6.setText("jCheckBox10");

        jProgressBar1.setName("test"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 53, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(0, 110, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jProgressBar9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jProgressBar12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jProgressBar10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jProgressBar11, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jProgressBar8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jProgressBar7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jProgressBar8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jProgressBar9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jProgressBar10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jProgressBar11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jProgressBar12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 29, Short.MAX_VALUE))
        );

        jCheckBox8.setText("jCheckBox3");

        jCheckBox7.setText("jCheckBox4");

        jCheckBox9.setText("jCheckBox2");

        actions.setText("Actions");

        Copier.setText("Copier");
        Copier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CopierActionPerformed(evt);
            }
        });
        actions.add(Copier);

        EjecterLesClesUSB.setText("Éjecter les clés USB");
        EjecterLesClesUSB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EjecterLesClesUSBActionPerformed(evt);
            }
        });
        actions.add(EjecterLesClesUSB);

        Formater.setText("Formater (default)");
        Formater.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FormaterActionPerformed(evt);
            }
        });
        actions.add(Formater);

        FormaterNTFS.setText("Formater (NTFS)");
        FormaterNTFS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FormaterNTFSActionPerformed(evt);
            }
        });
        actions.add(FormaterNTFS);

        FormaterFAT32.setText("Formater (FAT32)");
        FormaterFAT32.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FormaterFAT32ActionPerformed(evt);
            }
        });
        actions.add(FormaterFAT32);

        FormaterFAT.setText("Formater (FAT)");
        FormaterFAT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FormaterFATActionPerformed(evt);
            }
        });
        actions.add(FormaterFAT);

        barreMenu.add(actions);

        setJMenuBar(barreMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollEtat)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox3)
                            .addComponent(jCheckBox1)
                            .addComponent(jCheckBox2)
                            .addComponent(jCheckBox5))
                        .addGap(0, 676, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelSource)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(boutonParcourir))
                            .addComponent(jCheckBox4)
                            .addComponent(jCheckBox6))
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(labelDestination)
                                .addGap(60, 60, 60)
                                .addComponent(boutonNouveauDossier))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                                        .addComponent(jCheckBox7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jProgressBar4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jProgressBar5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jProgressBar6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jProgressBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jCheckBox12)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jCheckBox8, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jCheckBox9, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jCheckBox10, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jCheckBox11, javax.swing.GroupLayout.Alignment.TRAILING)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(20, 20, 20))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(boutonQuitter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(boutonCopier)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(scrollEtat, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSource)
                    .addComponent(boutonParcourir)
                    .addComponent(labelDestination)
                    .addComponent(boutonNouveauDossier))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jCheckBox8, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jCheckBox9, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jCheckBox10, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jCheckBox11, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jCheckBox12, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jProgressBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jProgressBar4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jProgressBar5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jProgressBar6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCheckBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCheckBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCheckBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCheckBox5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCheckBox6, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(boutonQuitter)
                            .addComponent(boutonCopier))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boutonQuitterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonQuitterActionPerformed
        System.exit(0);
    }//GEN-LAST:event_boutonQuitterActionPerformed

    private void CopierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopierActionPerformed
        this.getSelectedSupports();
        if (supports[0] == null) {
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Erreur, aucun support n'est sélectionné");
        } else {
            if (selectedFile == null) {
                this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Erreur, aucun fichier n'est sélectionné");
            } else {
                this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Les fichiers seront copiés sur les supports suivants : ");
                for (int i = 0; supports[i] != null ; i++) {
                    this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n " +supports[i].getName());
                }
                //this.duplicateur.createDossier(nomDossier, supports);
                this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Début de copie...");
                this.duplicateur.copySupports(selectedFile, supports, dossier, this, this.selectedProgressBar);
            }
        }
    }//GEN-LAST:event_CopierActionPerformed

    private void boutonParcourirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonParcourirActionPerformed
        this.selectedFile = this.duplicateur.selectFile();
        if (this.selectedFile != null) {
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\nLes fichiers suivant seront copiés : ");
            for (File f : this.selectedFile) {
                this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n"+f.getName());
            }
        }
        
    }//GEN-LAST:event_boutonParcourirActionPerformed

    private void boutonNouveauDossierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonNouveauDossierActionPerformed
        int returnValue;

        //CrÃ©ation du fileChooser
        JFileChooser fileChooser = new JFileChooser();
        //Activation du mode multi sÃ©lection
        fileChooser.setMultiSelectionEnabled(false);
        //Activation du mode de sÃ©lection des fichiers et des rÃ©pertoires
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //RÃ©cupÃ©ration de la valeur de retour
        returnValue = fileChooser.showOpenDialog(fileChooser);
        //VÃ©rifie si le choix est validÃ©
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            dossier = fileChooser.getSelectedFile();
        }
    }//GEN-LAST:event_boutonNouveauDossierActionPerformed

    private void boutonCopierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonCopierActionPerformed
        this.getSelectedSupports();
        if (supports[0] == null) {
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Erreur, aucun support n'est sélectionné");
        } else {
            if (selectedFile == null) {
                this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Erreur, aucun fichier n'est sélectionné");
            } else {
                this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Les fichiers seront copiés sur les supports suivants : ");
                for (int i = 0; supports[i] != null ; i++) {
                    this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n " +supports[i].getPath());
                    nbThread ++;
                }
                //this.duplicateur.createDossier(nomDossier, supports);
                this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Début de copie...");
                this.duplicateur.copySupports(selectedFile, supports, dossier, this, this.selectedProgressBar);
            }
        }
    }//GEN-LAST:event_boutonCopierActionPerformed



    public JTextArea getJTextAreaEtat() {
        jTextAreaEtat.setCaretPosition(jTextAreaEtat.getDocument().getLength());
        return this.jTextAreaEtat;
    }

    private void FormaterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FormaterActionPerformed
        // TODO add your handling code here:
        
        int response = JOptionPane.showConfirmDialog(null, 
                "Etes vous sur de vouloir formater (par défaut) les clés sélectionnés?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
          if (response == JOptionPane.NO_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          } else if (response == JOptionPane.YES_OPTION) {
                System.out.println("liste des disques amovibles à formater (par défaut) : ");
                for (int i = 0; i < allSupports.size(); i++) {
                    if(tabCheckBox[i].isSelected()){
                        int debut = tabCheckBox[i].getText().indexOf("(");
                        int fin = tabCheckBox[i].getText().indexOf(")");
                        String diskAFormater = tabCheckBox[i].getText().substring(debut+1, fin);
                        System.out.println(diskAFormater);
                        System.out.println("Début du formatage de " + diskAFormater);
                        this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage de " + diskAFormater + " en cours ... ");
                        try {  
                            String cmd = "ECHO. | FORMAT " + diskAFormater + " /Q /X"; 
                            System.out.println(cmd);
                            String[] commande = {"cmd.exe", "/C", cmd};
                            Process p = Runtime.getRuntime().exec(commande);
                            AfficheurFlux fluxSortie;
                            fluxSortie = new AfficheurFlux(p.getInputStream(), this.jTextAreaEtat, diskAFormater);
                            AfficheurFlux fluxErreur = new AfficheurFlux(p.getErrorStream(), this.jTextAreaEtat, diskAFormater);
                            
                            new Thread(fluxSortie).start();
                            new Thread(fluxErreur).start();

                        } catch (IOException e) {
                        }
                        finally{
                            System.out.println("Commande de formatage envoyée");
                        }
                    } else {
                    }
                }
          } else if (response == JOptionPane.CLOSED_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          }
    }//GEN-LAST:event_FormaterActionPerformed

    private void FormaterNTFSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FormaterNTFSActionPerformed
        // TODO add your handling code here: 
        
        int response = JOptionPane.showConfirmDialog(null, 
                "Etes vous sur de vouloir formater (NTFS) les clés sélectionnés?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
          if (response == JOptionPane.NO_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          } else if (response == JOptionPane.YES_OPTION) {
                System.out.println("liste des disques amovibles à formater (NTFS) : ");
                for (int i = 0; i < allSupports.size(); i++) {
                    if(tabCheckBox[i].isSelected()){
                        int debut = tabCheckBox[i].getText().indexOf("(");
                        int fin = tabCheckBox[i].getText().indexOf(")");
                        String diskAFormater = tabCheckBox[i].getText().substring(debut+1, fin);
                        System.out.println(diskAFormater); 
                        System.out.println("Début du formatage de " + diskAFormater);
                        this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage " + diskAFormater + " en cours ... ");
                        try {  
                            String cmd = "ECHO. | FORMAT " + diskAFormater + "/FS:NTFS /Q /X"; 
                            System.out.println(cmd);
                            String[] commande = {"cmd.exe", "/C", cmd};
                            Process p = Runtime.getRuntime().exec(commande);
                            AfficheurFlux fluxSortie;
                            fluxSortie = new AfficheurFlux(p.getInputStream(), this.jTextAreaEtat, diskAFormater);
                            AfficheurFlux fluxErreur = new AfficheurFlux(p.getErrorStream(), this.jTextAreaEtat, diskAFormater);
                            
                            new Thread(fluxSortie).start();
                            new Thread(fluxErreur).start();

                        } catch (IOException e) {
                        }
                        finally{
                            System.out.println("Commande de formatage envoyée");
                        }
                    } else {
                    }
                }
          } else if (response == JOptionPane.CLOSED_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          }
    }//GEN-LAST:event_FormaterNTFSActionPerformed

    private void FormaterFAT32ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FormaterFAT32ActionPerformed
        // TODO add your handling code here:
        
        int response = JOptionPane.showConfirmDialog(null, 
                "Etes vous sur de vouloir formater (FAT32) les clés sélectionnés?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
          if (response == JOptionPane.NO_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          } else if (response == JOptionPane.YES_OPTION) {
                System.out.println("liste des disques amovibles à formater (FAT32) : ");
                for (int i = 0; i < allSupports.size(); i++) {
                    if(tabCheckBox[i].isSelected()){
                        int debut = tabCheckBox[i].getText().indexOf("(");
                        int fin = tabCheckBox[i].getText().indexOf(")");
                        String diskAFormater = tabCheckBox[i].getText().substring(debut+1, fin);
                        System.out.println(diskAFormater);
                        System.out.println("Début du formatage de " + diskAFormater);
                        this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage " + diskAFormater + " en cours ... ");
                        try {  
                            String cmd = "ECHO. | FORMAT " + diskAFormater + "/FS:FAT32 /Q /X"; 
                            System.out.println(cmd);
                            String[] commande = {"cmd.exe", "/C", cmd};
                            Process p = Runtime.getRuntime().exec(commande);
                            AfficheurFlux fluxSortie;
                            fluxSortie = new AfficheurFlux(p.getInputStream(), this.jTextAreaEtat, diskAFormater);
                            AfficheurFlux fluxErreur = new AfficheurFlux(p.getErrorStream(), this.jTextAreaEtat, diskAFormater);
                            
                            new Thread(fluxSortie).start();
                            new Thread(fluxErreur).start();

                        } catch (IOException e) {
                        }
                        finally{
                            System.out.println("Commande de formatage envoyée");
                        }
                    } else {
                    }
                }
          } else if (response == JOptionPane.CLOSED_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          }
    }//GEN-LAST:event_FormaterFAT32ActionPerformed

    private void FormaterFATActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FormaterFATActionPerformed
        
        int response = JOptionPane.showConfirmDialog(null, 
                "Etes vous sur de vouloir formater (FAT) les clés sélectionnés?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
          if (response == JOptionPane.NO_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          } else if (response == JOptionPane.YES_OPTION) {
                System.out.println("liste des disques amovibles à formater (FAT) : ");
                for (int i = 0; i < allSupports.size(); i++) {
                    if(tabCheckBox[i].isSelected()){
                        int debut = tabCheckBox[i].getText().indexOf("(");
                        int fin = tabCheckBox[i].getText().indexOf(")");
                        String diskAFormater = tabCheckBox[i].getText().substring(debut+1, fin);
                        System.out.println(diskAFormater);
                        System.out.println("Début du formatage de " + diskAFormater);
                        this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage " + diskAFormater + " en cours ... ");
                        try {  
                            String cmd = "ECHO. | FORMAT " + diskAFormater + "/FS:FAT /Q /X"; 
                            System.out.println(cmd);
                            String[] commande = {"cmd.exe", "/C", cmd};
                            Process p = Runtime.getRuntime().exec(commande);
                            AfficheurFlux fluxSortie;
                            fluxSortie = new AfficheurFlux(p.getInputStream(), this.jTextAreaEtat, diskAFormater);
                            AfficheurFlux fluxErreur = new AfficheurFlux(p.getErrorStream(), this.jTextAreaEtat, diskAFormater);
                            
                            new Thread(fluxSortie).start();
                            new Thread(fluxErreur).start();

                        } catch (IOException e) {
                        }
                        finally{
                            System.out.println("Commande de formatage envoyée");
                        }
                    } else {
                    }
                }
          } else if (response == JOptionPane.CLOSED_OPTION) {
            System.out.println("Formatage annulé");
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Formatage annulé");
          }
    }//GEN-LAST:event_FormaterFATActionPerformed


    private void EjecterLesClesUSBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EjecterLesClesUSBActionPerformed
        this.duplicateur.ejectUsb();
        this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Les supports ont bien été ejectés");
        this.getJTextAreaEtat().setCaretPosition(this.getJTextAreaEtat().getDocument().getLength());
        updateSupports();
    }//GEN-LAST:event_EjecterLesClesUSBActionPerformed

    public JProgressBar[] getJProgressBars() {
        return tabProgressBar;
    }
    
    public int getNbThread() {
        return nbThread;
    }
    
    public void addNbThreadEnd() {
        nbThreadEnd++;
    }
    public int getNbThreadEnd() {
        return nbThreadEnd;
    }
    
    public void endCopy() {
         this.getJTextAreaEtat().setText(this.getJTextAreaEtat().getText()+"\n Copie terminée");
        this.getJTextAreaEtat().setCaretPosition(this.getJTextAreaEtat().getDocument().getLength());
        nbThread = 0;
        nbThreadEnd = 0;
        for(JProgressBar j : tabProgressBar) {
            j.setValue(0);
        }
        int response = JOptionPane.showConfirmDialog(null, 
                "La copie est terminée, voulez-vous éjecter les supports?", "Ejecter?",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (response == JOptionPane.YES_OPTION) {
            this.duplicateur.ejectUsb();
            this.jTextAreaEtat.setText(this.jTextAreaEtat.getText()+"\n Les supports ont bien été ejectés");
            this.getJTextAreaEtat().setCaretPosition(this.getJTextAreaEtat().getDocument().getLength());
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(InterfaceDuplicateur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InterfaceDuplicateur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InterfaceDuplicateur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InterfaceDuplicateur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfaceDuplicateur().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Copier;
    private javax.swing.JMenuItem EjecterLesClesUSB;
    private javax.swing.JMenuItem Formater;
    private javax.swing.JMenuItem FormaterFAT;
    private javax.swing.JMenuItem FormaterFAT32;
    private javax.swing.JMenuItem FormaterNTFS;
    private javax.swing.JMenu actions;
    private javax.swing.JMenuBar barreMenu;
    private javax.swing.JButton boutonCopier;
    private javax.swing.JButton boutonNouveauDossier;
    private javax.swing.JButton boutonParcourir;
    private javax.swing.JButton boutonQuitter;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox10;
    private javax.swing.JCheckBox jCheckBox11;
    private javax.swing.JCheckBox jCheckBox12;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JCheckBox jCheckBox7;
    private javax.swing.JCheckBox jCheckBox8;
    private javax.swing.JCheckBox jCheckBox9;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JProgressBar jProgressBar10;
    private javax.swing.JProgressBar jProgressBar11;
    private javax.swing.JProgressBar jProgressBar12;
    private javax.swing.JProgressBar jProgressBar2;
    private javax.swing.JProgressBar jProgressBar3;
    private javax.swing.JProgressBar jProgressBar4;
    private javax.swing.JProgressBar jProgressBar5;
    private javax.swing.JProgressBar jProgressBar6;
    private javax.swing.JProgressBar jProgressBar7;
    private javax.swing.JProgressBar jProgressBar8;
    private javax.swing.JProgressBar jProgressBar9;
    private javax.swing.JTextArea jTextAreaEtat;
    private javax.swing.JLabel labelDestination;
    private javax.swing.JLabel labelSource;
    private javax.swing.JScrollPane scrollEtat;
    // End of variables declaration//GEN-END:variables
}
